#include "adapi.h"

ADApi::ADApi()
{
    _instance = new AdMediaContext();
}

ADApi::~ADApi(){
    delete _instance;
}

QByteArray ADApi::GetBytes(const QString &sentence, const QString &filter_type, const QString &filter_attr, const QString &url){
    QString url0 = QString("%1?word=%2&type=%3&attr=%4").arg(url,sentence, filter_type, filter_attr);
    QNetworkRequest request;
    request.setUrl(url0);
    QNetworkAccessManager manage;
    QNetworkReply* reply =  manage.get(request);
    QEventLoop loop;
    QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()),Qt::DirectConnection);
    loop.exec();
    return reply->readAll();
}

QList<ADDataType> ADApi::DeseriesBytes(const QByteArray &bytes){
    return ADDataType::Deseries(bytes);
}

void ADApi::MediaRegistry(int media_id, int proj_id, const QString &media_cn, const QString &media_name, const QString &attr, int floor, const QString &area){
    AdMediaContext::GetInstance()->MediaRegistry(media_id, proj_id, media_cn, media_name, attr, floor, area);
}

void ADApi::MediasRegistry(int media_id, int proj_id, const QString &media_cn, const QString &media_name, const QString &attrs, int floor, const QString &area){
    AdMediaContext::GetInstance()->MediasRegistry(media_id, proj_id, media_cn, media_name, attrs, floor, area);
}

void ADApi::ScenesRegistry(int media_id, int proj_id,const QString &scenes, int floor, const QString &area){
     AdMediaContext::GetInstance()->ScenesRegistry(scenes, media_id, proj_id, floor, area);
}

void ADApi::ClearAllMedias(){
    AdMediaContext::GetInstance()->ClearAllMedias();
}

void ADApi::ClearMedia(int media_id, int proj_id, const QString &media_cn, const QString &media_name, const QString &attr, int floor, const QString &area){
    AdMediaContext::GetInstance()->ClearMedia(media_id, proj_id, media_cn, media_name, attr, floor, area);
}

QList<SimpleInfo> ADApi::GetAllRegistriedMedias(const QString &media_cn, const QString &media_name, const QString &attr, int floor, const QString &area){
   return AdMediaContext::GetInstance()->GetAllRegistriedMedias(media_cn, media_name, attr, floor, area);
}

QList<ADDataType> ADApi::FilterCommandS(const ADDataType &data, int &status){
    return AdMediaContext::GetInstance()->FilterCommandS(data, status);
}

QList<ADDataType> ADApi::FilterCommandS(QList<ADDataType> &datas, int &prior, int floor, QString area){
    return AdMediaContext::GetInstance()->FilterCommandS(datas, prior, floor, area);
}

QList<ADDataType> ADApi::ParseAttention(const QString &sentence, int &status, int floor, const QString &area, const QString &url, const QString &filter_type, const QString &filter_attr){
     //status: 0.无疑问结果, 1.网络错误, 2.解析失败, 3未有合适项, 4.场景匹配结果， 5.未指定设备名结果
    QByteArray bytes = GetBytes(sentence, filter_type, filter_attr, url);
    if(bytes.size() == 0){
        status = 1;
        QList<ADDataType> c0x;
        return c0x;
    }
    return ParseAttention(bytes, status, floor, area);
}

QList<ADDataType> ADApi::ParseAttention(const QByteArray &bytes, int &status, int floor, const QString &area){
    QList<ADDataType> resp = DeseriesBytes(bytes);


    //for(auto adtp: resp) adtp.PrintOutInfo();

    for(int i=resp.size()-1; i>=0; --i){
        if(!resp[i].Success) resp.removeAt(i);
    }

    if(resp.empty()){
        status = 2;
        QList<ADDataType> c0x;
        return c0x;
    }

    int prior;

    QList<ADDataType> result = FilterCommandS(resp, prior, floor, area);
    if(result.empty()){
        status = 3;
        QList<ADDataType> c0x;
        return c0x;
    }

    //prior: 3 指定设备名但未匹配上返回所有同类设备 ,4未定名称匹配 + 非场景匹配结果, 5.未定名称匹配 + 场景匹配结果, 6.名称确定匹配结果
    //status: 0.无疑问结果, 2.解析失败, 3未有合适项, 4.场景匹配结果， 5.未指定设备名结果, 6.指定设备名但未匹配上返回所有同类设备

    if(prior==3) status = 6;
    else if(prior==4) status = 5;
    else if(prior==5) status = 4;
    else if(prior==6) status = 0;
    return result;
}

//以下为非单例模式支持
void ADApi::MutiMediaRegistry(int media_id, int proj_id, const QString &media_cn, const QString &media_name, const QString &attr, int floor, const QString &area){
     _instance->MediaRegistry(media_id, proj_id, media_cn, media_name, attr, floor, area);
}

void ADApi::MutiMediasRegistry(int media_id, int proj_id, const QString &media_cn, const QString &media_name, const QString &attrs, int floor, const QString &area){
     _instance->MediasRegistry(media_id, proj_id, media_cn, media_name, attrs, floor, area);
}

void ADApi::MutiSceneRegistry(int media_id, int proj_id, const QString &scenes, int floor, const QString &area){
    _instance->ScenesRegistry(scenes, media_id, proj_id, floor, area);
}

void ADApi::MutiClearAllMedias(){
    _instance->ClearAllMedias();
}

void ADApi::MutiClearMedia(int media_id, int proj_id, const QString &media_cn, const QString &media_name, const QString &attr, int floor, const QString &area){
    _instance->ClearMedia(media_id, proj_id, media_cn, media_name, attr, floor, area);
}

QList<SimpleInfo> ADApi::MutiGetAllRegistriedMedias(const QString &media_cn, const QString &media_name, const QString &attr, int floor, const QString &area){
   return _instance->GetAllRegistriedMedias(media_cn, media_name, attr, floor, area);
}

QList<ADDataType> ADApi::MutiFilterCommandS(const ADDataType &data, int &status){
    return _instance->FilterCommandS(data, status);
}

QList<ADDataType> ADApi::MutiFilterCommandS(QList<ADDataType> &datas, int &prior, int floor, QString area){
    return _instance->FilterCommandS(datas, prior, floor, area);
}

QList<ADDataType> ADApi::MutiParseAttention(const QString &sentence, int &status, int floor, const QString &area, const QString &url, const QString &filter_type, const QString &filter_attr){
    QByteArray bytes = GetBytes(sentence, filter_type, filter_attr, url);
    if(bytes.size() == 0){
        status = 1;
        QList<ADDataType> c0x;
        return c0x;
    }
    QList<ADDataType> resp = DeseriesBytes(bytes);
    for(int i=resp.size()-1; i>=0; --i){
        if(!resp[i].Success) resp.removeAt(i);
    }
    if(resp.empty()){
        status = 2;
        QList<ADDataType> c0x;
        return c0x;
    }
    int prior;
    QList<ADDataType> result = MutiFilterCommandS(resp, prior, floor, area);
    if(result.empty()){
        status = 3;
        QList<ADDataType> c0x;
        return c0x;
    }

    if(prior==3) status = 6;
    else if(prior==4) status = 5;
    else if(prior==5) status = 4;
    else if(prior==6) status = 0;

    return result;
}
